package com.lrkj.property.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.SiteManag;
import com.lrkj.property.domain.SitePostManag;
import com.lrkj.property.mapper.SitePostManagMapper;
import com.lrkj.property.service.ISiteManagService;
import com.lrkj.property.service.ISitePostManagService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 场位管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-16
 */
@Service
public class SitePostManagServiceImpl implements ISitePostManagService {
    @Autowired
    private SitePostManagMapper sitePostManagMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private ISiteManagService siteManagService;

    /**
     * 查询场位管理
     *
     * @param id 场位管理ID
     * @return 场位管理
     */
    @Override
    public SitePostManag selectSitePostManagById(String id) {
        return sitePostManagMapper.selectSitePostManagById(id);
    }

    /**
     * 查询场位管理列表
     *
     * @param sitePostManag 场位管理
     * @return 场位管理
     */
    @Override
    public List<SitePostManag> selectSitePostManagList(SitePostManag sitePostManag) {
        return sitePostManagMapper.selectSitePostManagList(sitePostManag);
    }

    /**
     * 新增场位管理
     *
     * @param sitePostManag 场位管理
     * @return 结果
     */
    @Override
    public int insertSitePostManag(SitePostManag sitePostManag) {
        sitePostManag.setId(IdUtils.simpleUUID());
        sitePostManag.setCreateBy(SecurityUtils.getUsername());
        sitePostManag.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(sitePostManag);
        return sitePostManagMapper.insertSitePostManag(sitePostManag);
    }

    /**
     * 修改场位管理
     *
     * @param sitePostManag 场位管理
     * @return 结果
     */
    @Override
    public int updateSitePostManag(SitePostManag sitePostManag) {
        sitePostManag.setUpdateTime(DateUtils.getNowDate());
        sitePostManag.setUpdateBy(SecurityUtils.getUsername());
        this.setUnitInfo(sitePostManag);
        return sitePostManagMapper.updateSitePostManag(sitePostManag);
    }

    private void setUnitInfo(SitePostManag bean) {
        SiteManag parentBean = siteManagService.selectSiteManagById(bean.getSiteId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getSiteId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getId(), bean.getSitePostName(), bean.getSiteId(), OrgConstants.site_post, parentBean.getAncestors(), bean.getOrderNum());
    }

    /**
     * 批量删除场位管理
     *
     * @param ids 需要删除的场位管理ID
     * @return 结果
     */
    @Override
    public int deleteSitePostManagByIds(String[] ids) {
        orgTreeService.deleteOrgTreeByIds(ids);
        return sitePostManagMapper.deleteSitePostManagByIds(ids);
    }

    /**
     * 删除场位管理信息
     *
     * @param id 场位管理ID
     * @return 结果
     */
    @Override
    public int deleteSitePostManagById(String id) {
        return sitePostManagMapper.deleteSitePostManagById(id);
    }

    /**
     * 批量新增场位管理信息
     *
     * @param sitePostManags 场位管理对象
     */
    public int batchAddData(List<SitePostManag> sitePostManags) {
        return sitePostManagMapper.batchAddData(sitePostManags);
    }
}
